package com.cheer.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import com.cheer.common.Result;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * @author cheer
 */
@RestController
@RequestMapping("/tool")
public class ToolController {

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("生成根据redis key 值生成文件")
    @GetMapping("/createReport")
    public void createReport(@RequestParam String key) {
        try {
            Set<String> keys = stringRedisTemplate.keys(key);
            List<String> values = new ArrayList<>();
            for (String k : keys) {
                String value = stringRedisTemplate.opsForValue().get(k);
                values.add(value);
            }
            String fileName = "report_" + DateUtil.format(new Date(), "yyyyMMddHHmmss") + ".txt";
            // 测试环境
            String filePath = "/usr/local/" + fileName;
            FileUtil.writeLines(values, filePath, "UTF-8");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
